package com.bocloud.cmp.driver.providers.os;

import java.util.ArrayList;
import java.util.List;

import org.openstack4j.api.Builders;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.image.Image;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bocloud.cmp.driver.convertor.BeanConvert;
import com.bocloud.cmp.driver.convertor.os.ConvertImage;
import com.bocloud.cmp.driver.providers.ImageDriver;
import com.bocloud.cmp.model.ImageModel;
import com.bocloud.common.model.BsmResult;
import com.google.common.collect.Lists;

/**
 * Openstack镜像驱动
 * 
 * @author dmw
 *
 */
public class OSImageDriver extends OSDriver implements ImageDriver {

	private static final Logger logger = LoggerFactory.getLogger(OSImageDriver.class);

	/**
	 * 
	 */
	public OSImageDriver() {
		super();
	}

	public OSImageDriver(String endpoint, String username, String password, String tenant, String domain,
			String project, Version version) {
		super(endpoint, username, password, tenant, domain, project, version);
	}

	@Override
	public BsmResult list() {
		List<ImageModel> imageModels = Lists.newArrayList();
		List<? extends Image> images = Lists.newArrayList();
		if (!this.accessable()) {
			logger.error(this.getError());
			return new BsmResult(false, this.getError());
		}
		images = getClient().images().list();
		if (null == images || images.isEmpty()) {
			images = new ArrayList<>();
		}
		for (Image image : images) {
			BeanConvert<ImageModel, Image> convertImage = new ConvertImage();
			imageModels.add(convertImage.convertModel(image));
		}
		return new BsmResult(true, imageModels, "success");
	}

	@Override
	public BsmResult detail(String imageId) {
		if (!this.accessable()) {
			logger.error(this.getError());
			return new BsmResult(false, this.getError());
		}
		Image image = this.getClient().images().get(imageId);
		if (null == image) {
			return new BsmResult(false, "镜像不存在！");
		}
		BeanConvert<ImageModel, Image> convertImage = new ConvertImage();
		return new BsmResult(true, convertImage.convertModel(image), "success");
	}

	@Override
	public BsmResult remove(String imageId) {
		if (!this.accessable()) {
			logger.error(this.getError());
			return new BsmResult(false, this.getError());
		}
		ActionResponse response = this.getClient().images().delete(imageId);
		if (response.isSuccess()) {
			return new BsmResult(true, "删除失败!");
		} else {
			logger.error("remove snapshot [{}] error: {}", imageId, response.getFault());
			return new BsmResult(false, response.getFault());
		}
	}

	@Override
	public BsmResult modify(ImageModel image) {
		if (!this.accessable()) {
			logger.error(this.getError());
			return new BsmResult(false, this.getError());
		}
		Image osImage = Builders.image().name(image.getName()).isPublic(true).build();
		osImage.setId(image.getImageId());
		Image newImage = this.getClient().images().update(osImage);
		if (null != newImage) {
			return new BsmResult(true, "操作成功！");
		} else {
			logger.error("");
			return new BsmResult(false, "操作失败！");
		}
	}

}
